import 'dart:io';
import 'package:path/path.dart' as path;

String getLibraryPath(String libraryName) {
  try {
    String basePath;
    if (Platform.isMacOS || Platform.isIOS) {
      basePath = path.join(Directory.current.path, '${libraryName}_library', 'framework', 'lib$libraryName.dylib');
    } else if (Platform.isWindows) {
      basePath = path.join(Directory.current.path, '${libraryName}_library', 'Debug', '$libraryName.dll');
    } else {
      basePath = path.join(Directory.current.path, '${libraryName}_library', 'lib$libraryName.so');
    }

    // 检查文件是否存在，如果不存在抛出异常
    File libraryFile = File(basePath);
    if (!libraryFile.existsSync()) {
      throw UnsupportedError('Unknown platform: ${Platform.operatingSystem} Library file not found at $basePath');
    }

    return basePath;
  } catch (e) {
    // 优雅地处理异常，可以根据需要记录日志或者抛出自定义异常
    // 例如，这里只是简单打印异常信息
    print('Error while getting library path: $e');
    // 可以选择返回一个默认路径，或者根据情况抛出异常
    throw Exception('Unable to determine library path.');
  }
}

// dart String 转 c char[] 数组 这里需要传入初始化过的空数组
// void cArrayFromDartString(String string, Array<Char> cArray) {
//   Pointer<Utf8> pUtf8 = string.toNativeUtf8();
//   Pointer<Uint8> head = pUtf8.cast<Uint8>();
//   var i = 0;
//   while (head[i] != 0) {
//     cArray[i] = head[i];
//     i++;
//   }
// }

//调用方式
// cArrayFromDartString("123", pperson.ref.name);
